package test;

import java.io.BufferedReader;
import java.io.FileReader;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLDecoder;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

public class Program {

	private static String findYouTubeVideoId(String url) {
		String pattern = "(?<=watch\\?v=|/videos/|/vi/|embed\\/)[^#\\&\\?\\/]*";

		Pattern compiledPattern = Pattern.compile(pattern);
		Matcher matcher = compiledPattern.matcher(url);

		if (matcher.find()) {
			return matcher.group();
		} else {
			return url;
		}

	}

	private static String getVimeoThumbNailURL(String videoId) {
		videoId = "61825987";
		String urlString = "http://vimeo.com/api/v2/video/" + videoId + ".xml";
		URL url = null;
		URLConnection conn = null;

		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = null;
		Document doc = null;

		String res = "";

		try {
			url = new URL(urlString);
			conn = url.openConnection();
			builder = factory.newDocumentBuilder();
			doc = builder.parse(conn.getInputStream());
			// doc.getDocumentElement().normalize();
			NodeList listOfVideos = doc
					.getElementsByTagName("thumbnail_medium");
			// NodeList listOfVideos =
			// doc.getElementsByTagName("thumbnail_small");
			// NodeList listOfVideos =
			// doc.getElementsByTagName("thumbnail_large");

			res = listOfVideos.item(0).getTextContent();

		} catch (Exception e) {
			e.printStackTrace();
		}

		return res;

	}

	private static String getVimeoVideoId(String url) {
		String res = "";

		res = url.substring(url.lastIndexOf("/") + 1, url.length());

		return res;
	}

	private static String getDurationsString(long currPos, long duration) {
		String res = "";

		Date date = new Date(currPos);
		DateFormat formatter = new SimpleDateFormat("mm:ss");
		String dateFormatted = formatter.format(date);

		res += dateFormatted;
		res += " / ";
		dateFormatted = formatter.format(new Date(duration));
		res += dateFormatted;
		return res;
	}

	private static String extractLinks(String message) {
		// surround text with custom tags
		// "_l:_", "_:l_"

		String mymessage = "";

		// Pattern MY_PATTERN =
		// Pattern.compile("([^ .]*://[^ \\t\\r\\n\\v\\f]+)");
		Pattern MY_PATTERN = Pattern
				.compile("\\(?\\b(http://|https://|www[.])[-A-Za-z0-9+&@#/%?=~_()|!:,.;]*[-A-Za-z0-9+&@#/%=~_()|]");
		Matcher m = MY_PATTERN.matcher(message);

		while (m.find()) {
			String possibleLink = m.group();
			mymessage += " " + possibleLink;
			System.out.println(possibleLink);
		}
		return mymessage;
	}

	private static String extractYoutubeId(String url) {

		if (!url.contains("youtube"))
			return null;

		String id = null;
		try {
			String query = new URL(url).getQuery();
			if (query != null) {
				String[] param = query.split("&");
				for (String row : param) {
					String[] param1 = row.split("=");
					if (param1[0].equals("v")) {
						id = param1[1];
					}
				}
			} else {
				if (url.contains("embed")) {
					id = url.substring(url.lastIndexOf("/") + 1);
				}
			}
			String[] param = id.split("[\\r\\n ]+");
			id = param[0];
		} catch (Exception ex) {
		}
		return id;
	}

	private static String extractYoutubeId2(String url) {
		if (!url.contains("youtube"))
			return null;

		String id = null;
		try {
			String query = new URL(url).getQuery();
			if (query != null) {
				String[] param = query.split("&");
				for (String row : param) {
					String[] param1 = row.split("=");
					if (param1[0].equals("v")) {
						id = param1[1];
					}
				}
			} else {
				if (url.contains("embed")) {
					id = url.substring(url.lastIndexOf("/") + 1);
				}
			}
			String[] param = id.split("[\\r\\n ]+");
			id = param[0];
		} catch (Exception ex) {
		}
		return id;
	}

	private static void dateTest() {
		String urlCoded = "http://127.0.0.1:5000/3813/Manager%20Playbook_Planning%20-%20Terms%20of%20Engagement.pdf";
		String urlEncoded = "";
		try {
			urlEncoded = URLDecoder.decode(urlCoded, "UTF-8");
		} catch (Exception e) {
			// TODO: handle exception
		}
		System.out.println(urlEncoded);
	}
	
	private static boolean polindrom(String str) {
		char[] chars = str.toCharArray();
		int length = str.length();
		for (int i = 0; i < length / 2; i++) {
			if(chars[i] != chars[length - i -1])
				return false;
		}
		
		return true;
	} 
	
	private static String dateTest(long timestamp) {
		DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");

		if (dateFormat.format(timestamp)
				.equals(
					dateFormat.format(System.currentTimeMillis() )
						)) {
			DateFormat timeFormat = new SimpleDateFormat("HH:mm");

			return timeFormat.format(timestamp);
		}
		return dateFormat.format(timestamp);
	} 
	
	public static void main(String[] args) {
		
		SimpleDateFormat format = new SimpleDateFormat( "MM/dd/yyyy", Locale.UK );
        try {
			Date date = format.parse( "1/1/1970" );
			System.out.println(date.toString());
			format = new SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ss", Locale.UK );
            String dateString = format.format( date );
            System.out.println(dateString);
		} catch (ParseException e) {
			e.printStackTrace();
		}
	}

}
